<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
</style>

<body>

    <script src="./js/vue.js"></script>
    <div id='app'>
        <reduce @child-reduce="result" @child-add="result" :num="num">
            <div style="margin: 0 10px;" >{{num}}</div>
        </reduce>
    </div>
    <template id="btn">
        <div style="display: flex;">
            <button @click="Reduce">-</button>
            <slot></slot>
            <button @click="add">+</button> 
        </div>
    </template>
    <script>

        Vue.component('reduce', {
            data: function () {
                return {
                    num1: this.num
                }
            },
            props:['num'],
            template: "#btn",
            methods: {
                Reduce(){
                    if(this.num1>0){
                        this.num1--;
                        this.$emit('child-reduce',this.num1)
                    }
                },
                add(){
                    this.num1++;
                    this.$emit("child-add",this.num1)
                    console.log("加操作",this.num1);
                }
            },
        })
        let app = new Vue({
            el: "#app",
            data: {
                num: 0
            },methods:{
                result(num){
                    console.log("子组件传过来的",num);
                    this.num=num;
                }
            }
        })
    </script>
</body>

</html>